package com.paishen.peiniwan.module.profile;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.paishen.common.util.BaseUtil;
import com.paishen.peiniwan.BaseActivity;
import com.paishen.peiniwan.R;
import com.paishen.common.widget.TitleLayout;

/**
 * Created by peng on 2015/12/17.
 */
public class ProfileInfoEditSignActivity extends BaseActivity {

    public static final String EXTRA_SIGN_CONTENT = "sign_content";

    private TextView txSign1, txSign2, txSign3, txSign4;
    private EditText edSign;
    private TextView txLimitedCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_info_edit_sign);

        findViewById(R.id.lay_container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseUtil.hideSoftInputFromWindowAlways(ProfileInfoEditSignActivity.this);
            }
        });

        TitleLayout layTitle = (TitleLayout) findViewById(R.id.lay_title);
        layTitle.pushContent(makeBackView(), getString(R.string.profile_intro), null, false);

        edSign = (EditText) findViewById(R.id.ed_sign);
        String signContent = getIntent().getStringExtra(EXTRA_SIGN_CONTENT);
        if(!TextUtils.isEmpty(signContent))edSign.setText(signContent);
        txLimitedCount = (TextView) findViewById(R.id.tx_limited_count);
        edSign.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                final int maxLength = 20;
                txLimitedCount.setText(String.valueOf(maxLength - edSign.getText().length()));
            }
        });

        txSign1 = (TextView) findViewById(R.id.tx_sign_1);
        txSign2 = (TextView) findViewById(R.id.tx_sign_2);
        txSign3 = (TextView) findViewById(R.id.tx_sign_3);
        txSign4 = (TextView) findViewById(R.id.tx_sign_4);
        String[] profile_signs = getResources().getStringArray(R.array.profile_signs);
        txSign1.setText(profile_signs[0]);
        txSign2.setText(profile_signs[1]);
        txSign3.setText(profile_signs[2]);
        txSign4.setText(profile_signs[3]);

        View.OnClickListener onAutoInputClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edSign.setText(((TextView) v).getText());
            }
        };

        txSign1.setOnClickListener(onAutoInputClickListener);
        txSign2.setOnClickListener(onAutoInputClickListener);
        txSign3.setOnClickListener(onAutoInputClickListener);
        txSign4.setOnClickListener(onAutoInputClickListener);
    }

    @Override
    public void onBackPressed() {
        Intent intent = getIntent();
        String signContent = edSign.getText().toString();
        if (TextUtils.isEmpty(signContent)) {
            signContent = edSign.getHint().toString();
        }
        intent.putExtra(EXTRA_SIGN_CONTENT, signContent);
        setResult(RESULT_OK, intent);
        finish();
    }
}
